<?php
/**
 * sunjing@shopex.cn

 */

class erpapi_wms_matrix_jd_response_reship extends erpapi_wms_response_reship
{
    public function status_update($params)
    {
        $data = parent::status_update($params);
        if($data['status'] == 'FINISH' && empty($data['items'])) {
            $reshipData = app::get('ome')->model('reship')->db_dump(array('reship_bn' => $data['reship_bn']), 'reship_id');
            if (!$reshipData) {
                $this->__apilog['result']['msg'] = '没有该退货单';
                return false;
            }
            $itemFilter = array('reship_id' => $reshipData['reship_id'], 'return_type' => array('return', 'refuse'));
            $modelReshipItem = app::get('ome')->model('reship_items');
            $items = $modelReshipItem->getList('num,bn,item_id', $itemFilter);
            $reship_items = array();
            if ($items) {
                foreach ($items as $key => $val) {
                    if (!$val['bn']) continue;
                    $reship_items[$val['bn']]['bn'] = $val['bn'];
                    $reship_items[$val['bn']]['normal_num'] = (int)$reship_items[$val['bn']]['normal_num'] + (int)$val['num'];
                    $reship_items[$val['bn']]['defective_num'] = 0;
                }
            } else {
                $this->__apilog['result']['msg'] = '该退货明细已处理或缺少';
                return false;
            }
            $data['items'] = $reship_items;
        }
        return $data;
    }
}